<template>
  <div
    class="xtx-carousel"
    @mouseenter="removeInterval"
    @mouseleave="openInterval"
  >
    <ul class="carousel-body">
      <li
        class="carousel-item"
        :class="{ fade: currentIndex === index }"
        v-for="(item, index) in imgList"
        :key="item.id"
      >
        <RouterLink :to="item.hrefUrl">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <a @click="toggle(-1)" href="javascript:;" class="carousel-btn prev"
      ><i class="iconfont icon-angle-left"></i
    ></a>
    <a @click="toggle(1)" href="javascript:;" class="carousel-btn next"
      ><i class="iconfont icon-angle-right"></i
    ></a>
    <div class="carousel-indicator">
      <span
        v-for="i in imgList.length"
        :key="i"
        :class="{ active: currentIndex === i - 1 }"
        @mouseenter="currentIndex = i - 1"
      ></span>
    </div>
  </div>
</template>

<script>
import { onBeforeUnmount, ref, watch } from 'vue'

export default {
  name: 'XtxCarousel',
  props: {
    imgList: {
      type: Array,
      default: () => []
    },
    duration: {
      type: Number,
      default: 3000
    },
    autoPlay: {
      type: Boolean,
      default: false
    }
  },
  setup (props, context) {
    // 当前显示图片的序号
    const currentIndex = ref(0)

    // 点击切换轮播图
    function toggle (num) {
      if (props.imgList.length === 0) return
      currentIndex.value += num
      if (currentIndex.value >= props.imgList.length) {
        currentIndex.value = 0
      }
      if (currentIndex.value < 0) {
        currentIndex.value = props.imgList.length - 1
      }
    }

    let _interval = null

    // 定时器开启
    function openInterval () {
      removeInterval()
      _interval = setInterval(() => {
        toggle(1)
      }, props.duration)
    }

    // 清除自动播放
    function removeInterval () {
      if (_interval) {
        clearInterval(_interval)
      }
    }

    // 保存 侦听器 便于后期销毁
    const watchAutoPlay = watch(
      () => props.autoPlay,
      newVal => {
        if (newVal) {
          openInterval()
        } else {
          removeInterval()
        }
      },
      { immediate: true }
    )

    const watchCurrent = watch(
      () => currentIndex,
      newVal => {
        context.emit('current', newVal.value)
      },
      {
        immediate: true,
        deep: true
      }
    )

    // 组件销毁前清除定时器和监听器
    onBeforeUnmount(() => {
      removeInterval()
      watchAutoPlay()
      watchCurrent()
    })
    return { currentIndex, toggle, removeInterval, openInterval }
  }
}
</script>
<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  max-height: 500px;

  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      max-height: 500px;

      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
